public class ThreadDemo4 {
    //自定义boolean类型的变量为标志符
    private static  boolean fla = false;
    public static void main(String[] args) throws InterruptedException {
        //实现匿名内部类,创建Runnable子类对象
        Thread t = new Thread(new Runnable() {

            @Override
            public void run() {
                //条件fla为真运行,
                while (!fla){
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    System.out.println("正在运行run");
                }
            }
        });
        //启动线程
        t.start();
        for (int i = 0; i < 5; i++) {

            Thread.sleep(1000);
            System.out.println("正在运行main方法");
        }
        //主线程for循环执行完，设置fla=true，此时在执行匿名内部类run时循环条件为假跳出循环
        fla=true;
    }
}
